package com.hlj.ncp.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hlj.ncp.security.entity.Inspection;
import com.hlj.ncp.security.mapper.InspectionMapper;
import com.hlj.ncp.security.service.IInspectionService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 安全巡检Service业务层处理
 */
@Service
public class InspectionServiceImpl extends ServiceImpl<InspectionMapper, Inspection> implements IInspectionService {

    /**
     * 查询安全巡检列表
     * 
     * @param inspection 安全巡检信息
     * @return 安全巡检
     */
    @Override
    public List<Inspection> selectInspectionList(Inspection inspection) {
        LambdaQueryWrapper<Inspection> queryWrapper = new LambdaQueryWrapper<>();
        if (inspection != null) {
            // 条件过滤
            if (inspection.getInspectionCode() != null && !"".equals(inspection.getInspectionCode())) {
                queryWrapper.like(Inspection::getInspectionCode, inspection.getInspectionCode());
            }
            if (inspection.getInspectionArea() != null && !"".equals(inspection.getInspectionArea())) {
                queryWrapper.like(Inspection::getInspectionArea, inspection.getInspectionArea());
            }
            if (inspection.getInspector() != null && !"".equals(inspection.getInspector())) {
                queryWrapper.like(Inspection::getInspector, inspection.getInspector());
            }
            if (inspection.getInspectionType() != null && !"".equals(inspection.getInspectionType())) {
                queryWrapper.eq(Inspection::getInspectionType, inspection.getInspectionType());
            }
            if (inspection.getInspectionResult() != null && !"".equals(inspection.getInspectionResult())) {
                queryWrapper.eq(Inspection::getInspectionResult, inspection.getInspectionResult());
            }
            if (inspection.getStatus() != null && !"".equals(inspection.getStatus())) {
                queryWrapper.eq(Inspection::getStatus, inspection.getStatus());
            }
        }
        // 默认按巡检时间降序排序
        queryWrapper.orderByDesc(Inspection::getInspectionTime);
        return list(queryWrapper);
    }

    /**
     * 查询安全巡检详细
     * 
     * @param inspectionId 安全巡检ID
     * @return 安全巡检
     */
    @Override
    public Inspection selectInspectionById(Long inspectionId) {
        return getById(inspectionId);
    }

    /**
     * 新增安全巡检
     * 
     * @param inspection 安全巡检信息
     * @return 结果
     */
    @Override
    public boolean insertInspection(Inspection inspection) {
        // 生成巡检编号，格式：INS + 年月日时分秒
        String currentTime = LocalDateTime.now().toString().replaceAll("[^0-9]", "").substring(0, 14);
        inspection.setInspectionCode("INS" + currentTime);
        
        // 如果没有设置巡检时间，则使用当前时间
        if (inspection.getInspectionTime() == null) {
            inspection.setInspectionTime(LocalDateTime.now());
        }
        
        return save(inspection);
    }

    /**
     * 修改安全巡检
     * 
     * @param inspection 安全巡检信息
     * @return 结果
     */
    @Override
    public boolean updateInspection(Inspection inspection) {
        return updateById(inspection);
    }

    /**
     * 批量删除安全巡检
     * 
     * @param inspectionIds 需要删除的安全巡检ID
     * @return 结果
     */
    @Override
    public boolean deleteInspectionByIds(Long[] inspectionIds) {
        return removeByIds(Arrays.asList(inspectionIds));
    }

    /**
     * 删除安全巡检信息
     * 
     * @param inspectionId 安全巡检ID
     * @return 结果
     */
    @Override
    public boolean deleteInspectionById(Long inspectionId) {
        return removeById(inspectionId);
    }
    
    /**
     * 获取安全检查统计数据
     * 
     * @return 统计数据
     */
    @Override
    public Map<String, Object> getInspectionStats() {
        Map<String, Object> stats = new HashMap<>();
        
        // 统计总巡检次数
        long total = count();
        stats.put("totalCount", total);
        
        // 统计异常巡检次数
        LambdaQueryWrapper<Inspection> abnormalQuery = new LambdaQueryWrapper<>();
        abnormalQuery.eq(Inspection::getInspectionResult, "1"); // 异常
        long abnormalCount = count(abnormalQuery);
        stats.put("abnormalCount", abnormalCount);
        
        // 统计待处理的异常数量
        LambdaQueryWrapper<Inspection> pendingQuery = new LambdaQueryWrapper<>();
        pendingQuery.eq(Inspection::getInspectionResult, "1")  // 异常
                    .eq(Inspection::getStatus, "0");  // 待处理
        long pendingCount = count(pendingQuery);
        stats.put("pendingCount", pendingCount);
        
        // 统计处理中的异常数量
        LambdaQueryWrapper<Inspection> processingQuery = new LambdaQueryWrapper<>();
        processingQuery.eq(Inspection::getInspectionResult, "1")  // 异常
                       .eq(Inspection::getStatus, "1");  // 处理中
        long processingCount = count(processingQuery);
        stats.put("processingCount", processingCount);
        
        // 统计已处理的异常数量
        LambdaQueryWrapper<Inspection> resolvedQuery = new LambdaQueryWrapper<>();
        resolvedQuery.eq(Inspection::getInspectionResult, "1")  // 异常
                     .eq(Inspection::getStatus, "2");  // 已处理
        long resolvedCount = count(resolvedQuery);
        stats.put("resolvedCount", resolvedCount);
        
        // 计算安全指数 (正常巡检占比，100表示全部正常，0表示全部异常)
        double safetyIndex = 100.0;
        if (total > 0) {
            safetyIndex = (total - abnormalCount) * 100.0 / total;
        }
        stats.put("safetyIndex", Math.round(safetyIndex * 100) / 100.0);
        
        return stats;
    }
} 